package com.woniuxy.background.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.advice.SysLog;
import com.woniuxy.background.common.RestResult;
import com.woniuxy.background.pojo.Admin;
import com.woniuxy.background.pojo.Role;
import com.woniuxy.background.service.AdminService;
import com.woniuxy.background.service.RolesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/roles")
public class RolesController {

    @Autowired
    private RolesService rolesService;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys:role:list")
    public RestResult list(@RequestParam("page")Integer currentPage, @RequestParam("limit")Integer pageSize){
        List<Role> list = rolesService.findByPage(currentPage,pageSize);
        PageInfo info = new PageInfo(list);
        return RestResult.success(info);
    }
    @RequestMapping("/listAll")
    @ResponseBody
    @RequiresPermissions("sys:role:list")
    public RestResult listAll(){
        List<Role> list = rolesService.selectRoles();
        return RestResult.success(list);
    }

    @PostMapping("/saveRoles")
    @ResponseBody
    @SysLog(value = "新增角色")
    @RequiresPermissions("sys:role:save")
    public RestResult saveRoles(Role role, String menuId){
        System.out.println(role);
        //判断角色的名称不能重复
        Role dbRole = rolesService.findRolesByName(role.getRoleName());
        if(dbRole != null){
            return RestResult.failed("角色名称已经存在");
        }
        try {
            rolesService.saveRoles(role,menuId);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("添加失败");
        }
        return RestResult.success();
    }

    /**
     * 导航到编辑角色的页面
     * @param roleId
     * @param modelAndView
     * @return
     */
    @GetMapping("/editRole/{roleId}")
    public ModelAndView editRole(@PathVariable("roleId")int roleId, ModelAndView modelAndView){
        //查询角色信息
        Role role = rolesService.findRolesById(roleId);
        modelAndView.addObject("roles",role);
        modelAndView.setViewName("/sys/editRole");
        return modelAndView;
    }

    /**
     * 更新角色
     * @return
     */
    @PutMapping("/updateRole")
    @ResponseBody
    @SysLog(value = "更新角色")
    @RequiresPermissions("sys:role:update")
    public RestResult updateRole(Role role, String menuId){
        if(rolesService.checkRoleName(role)){
            return RestResult.failed("该角色名称已经存在");
        }
        try {
            rolesService.updateRoles(role,menuId);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("更新失败");
        }
        return RestResult.success();
    }
    @DeleteMapping("/delete/{roleId}")
    @ResponseBody
    @SysLog(value = "删除角色")
    @RequiresPermissions("sys:role:delete")
    public RestResult deleteUser(@PathVariable("roleId") int roleId){
        try {
            List<Admin> admins = adminService.selectAdminByRoleId(roleId);
            for (Admin a : admins ){
                adminService.deleteByPrimaryKey(a.getAdminId());
            }
            rolesService.deleteByPrimaryKey(roleId);
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.failed("删除失败！");
        }
        return RestResult.success("删除成功！");
    }


}
